<template>
  <div class="tc fs-6 p-5"></div>

  <!-- 数据表格 -->
  <div>
    <NDataTable :loading="loading" :data="list" :columns="columns"></NDataTable>
  </div>

  <!-- 分页信息 -->
  <div>
    <NFlex justify="center"
      ><NPagination
        v-model:page="page.pageNumber"
        :page-size="page.pageSize"
        :item-count="page.total"
        @update-page="query"
      ></NPagination
    ></NFlex>
  </div>

  <!-- 删除的对话框 -->
  <NModal @after-leave="requery" v-model:show="dvisible" preset="dialog" title="帖子删除">
    <div>
      <NForm>
        <NFormItem>
          <NInput v-model:value="reason" type="textarea" placeholder="请输入删除理由"></NInput>
        </NFormItem>
      </NForm>
    </div>
    <template #action>
      <NSpace>
        <NButton type="error" @click="dvisible = false">关闭</NButton>
        <NButton :loading="loading" type="primary" @click="Delete">确认删除</NButton>
      </NSpace>
    </template>
  </NModal>

  <!-- 查看举报原因 -->
  <NModal @after-leave="requery" v-model:show="jvisible" preset="dialog" title="帖子举报原因">
    <div>
      <!-- {{ jlist }} -->
      <div v-for="d in jlist" :key="d.info">
        <span>{{ d.info }}</span>
        - 举报时间：(
        <span>{{ Tools.formatDate(d.created) }}</span>
        )
      </div>
    </div>
  </NModal>
</template>

<script setup lang="ts">
import { ref, h, onMounted } from 'vue'
import {
  useMessage,
  NDataTable,
  NButton,
  NPagination,
  NFlex,
  NSpace,
  NForm,
  NFormItem,
  NInput,
  NModal,
} from 'naive-ui'
import type { DataTableColumn } from 'naive-ui'
import type { BaseDataResult, BaseResult, PageBean, PageVo } from '@/types/BaseResult'
import { Api } from '@/types/Api'
import type { TbMessage, TbMessageReport } from '@/types/message'
import { useRouter } from 'vue-router'
import { Tools } from '@/types/Tools'
const router = useRouter()
const message = useMessage()

const loading = ref(false)

//#region 详情
const toDetail = (mid: number) => {
  router.push(`/message/${mid}`)
}
//#endregion

// #region 查询的部分
const page = ref({ pageNumber: 1, pageSize: 8 } as PageBean)
const list = ref([] as Array<TbMessage>)

const columns = ref([
  {
    title: '标题',
    key: 'title',
  },
  {
    title: '删除理由',
    key: 'reason',
  },
  {
    title: '状态',
    key: 'deleted',
    render(row) {
      return row.deleted === 'n' ? '等待处理' : '删除'
    },
  },
  {
    title: '操作',
    key: 'action',
    render(row) {
      return h(NSpace, {}, [
        h(
          NButton,
          {
            size: 'small',
            type: 'primary',
            onClick: () => {
              toDetail(row.mid)
            },
          },
          '查看帖子',
        ),
        h(
          NButton,
          {
            size: 'small',
            type: 'warning',
            onClick: () => JbYY(row),
          },
          '查看举报原因',
        ),
        h(
          NButton,
          {
            size: 'small',
            type: 'error',
            onClick: () => showDelete(row),
          },
          '删除',
        ),
      ])
    },
  },
] as DataTableColumn<TbMessage>[])

const requery = () => {
  page.value.pageNumber = 1
  query()
}

const query = () => {
  loading.value = true
  Api.get(
    `/admin/message/${page.value.pageSize}/${page.value.pageNumber}`,
    {},
    (data: BaseDataResult<PageVo<TbMessage>>) => {
      loading.value = false
      if (data.success) {
        list.value = data.data.list
        page.value = data.data.pageBean
        return
      }
      message.error(data.message)
    },
  )
}

onMounted(() => {
  query()
})
// #endregion

// #region 删除的部分
const dvisible = ref(false)
const reason = ref('')
const delinfo = ref({} as TbMessage)
const showDelete = (row: TbMessage) => {
  dvisible.value = true
  delinfo.value = Tools.concatJson(row) as TbMessage
}

const Delete = () => {
  loading.value = true
  Api.delete(
    `/admin/message`,
    { mid: delinfo.value.mid, reason: reason.value },
    (data: BaseResult) => {
      loading.value = false
      if (data.success) {
        message.success('删除成功')
        dvisible.value = false
        return
      }
      message.error(data.message)
    },
  )
}

// #endregion

//  #region 查看举报原因的部分
const jpage = ref({ pageNumber: 1, pageSize: 8 } as PageBean)
const jlist = ref([] as Array<TbMessageReport>)
const jvisible = ref(false)
const JbYY = (row: TbMessage) => {
  loading.value = true
  Api.get(
    `/admin/message/${row.mid}/${page.value.pageSize}/${page.value.pageNumber}`,
    {},
    (data: BaseDataResult<PageVo<TbMessageReport>>) => {
      loading.value = false
      if (data.success) {
        jpage.value = data.data.pageBean
        jlist.value = data.data.list
        jvisible.value = true
        return
      }
      message.error(data.message)
    },
  )
}
//  #endregion
</script>
<style scoped></style>
